
var userList = {};
var userOnline = 0;
//var userList = new Array();
var c = require('../majiang_server/config.json');
/**用来表示玩家的状态， */
var userState = {
    idle: 0,
    ready: 1,
    fight: 2,
    build: 3,
    leave: 4,
    invite: 5,
};
exports.createPlayer = function (data, tsocket, useridd) {
    var tuserId = data.account;
    var nickname = data.name;
    var theadimg = data.headimg;
    var currentPlayer = {
        //  id: ws.id,
        //用来保存玩家的信息
        socket: tsocket,
        name: nickname,
        userId: useridd,
        sex: data.sex,
        fightUserId: -1,
        //玩家连接服务器
        isLink:true,
        //来表示玩家状态
        userState: userState.idle,
        //headimg
        headimg: theadimg,
        lastHeartbeat: new Date().getTime(),
        //表示玩家是否为AI
        isAI: false,
        //玩家邀请函，-1表示没有，其他数字表示发送者userId
        invitation: -1,
        //玩家回合数
        roundLevel: 0,
        openid: tuserId,
        watingStartTime: 0,
        roundStartTime: 0,
        roundBuildTime: 0,
        /**玩家持有金币 */
        gold: 0,
        /**当前回合最多可得金币 */
        canGetGold: 0,
        /**玩家是否已准备进入下一回合 */
        isReadyToNext: false,

    };
    userList[useridd] = currentPlayer;
    tsocket.userId = useridd;
    console.log("创建玩家成功" + useridd);
    userOnline++;
};

exports.addAI = function (user) {
    userList[user.userId] = user;
};
exports.del = function (userId) {
    delete userList[userId];
    userOnline--;
};

exports.get = function (userId) {
    return userList[userId];
};

exports.isOnline = function (userId) {
    var data = userList[userId];
    if (data != null) {
        return true;
    }
    return false;
};

exports.getOnlineCount = function () {
    return userOnline;
}



//将玩家从世界踢出
exports.kickUser = function (user) {
    if (user == null)
        return;
    if (user.isAI)
        return;

    exports.del(user.userId);

};


/**
 * 用于重置玩家的状态
 * @param {} roomId 
 */
exports.resetPlayer = function (player) {

    player.roundLevel = 0;
    // player.roomId=0;
    player.roundStartTime = 0;

    player.userState = userState.idle;

    player.fightUserId = -1;
}
/**重置战斗玩家的回合数 */
exports.resetFightingPlayer = function (player) {

    player.roundLevel = 0;
    // player.roomId=0;

}
/**遍历所有玩家 */
exports.viewAllUser = function () {
    //console.log("查看在线玩家：")
    let userNum = 0;
    for (let key in userList) {
        userNum++;
        //console.log(key+"玩家心跳差："+Math.abs(userList[key].heartUp - userList[key].heartDown));
        if (!userList[key].isLink) {
            console.log(key + "玩家失去联系");
            userList[key].userState = userState.leave;
            exports.kickUser(userList[key]);
        } else {
            console.log(key + "号玩家当前状态:" + userList[key].userState);
            //suserList[key].isLink = false;
            console.log(userList[key].isLink);
        }
    }
    return userNum;
    //console.log("查看结束。");
}




